package threeGroup.service.impl;

import com.literature.threeGroup.mapper.MenuMapper;
import com.literature.threeGroup.mapper.ParamMapper;
import com.literature.threeGroup.mapper.PurchaseMapper;
import com.literature.threeGroup.pojo.Param;
import com.literature.threeGroup.pojo.Purchase;
import com.literature.threeGroup.pojo.PurchaseWithBLOBs;
import com.literature.threeGroup.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import threeGroup.service.PurchaseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author VULCAN
 * @create 2020-01-07 14:30
 * @desc
 **/
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private ParamMapper paramMapper;

    @Override
    public Result addPurchase(PurchaseWithBLOBs purchase) {

        purchaseMapper.insertSelective(purchase);
        return new Result(true,"荐购成功！");
    }

    @Override
    public Result selectRepeation(Purchase purchase) {
        //单条荐购
        List<Purchase> purchaseList=purchaseMapper.selectRepetition(purchase.getTitle(),purchase.getPisbn(),purchase.getPissn(),purchase.getEisbn(),purchase.getEissn());
        if(purchaseList.size()>0){
            return  new Result(false,"完全重复，不支持建构");
        }else{
            List<Purchase> likeRepetition=purchaseMapper.selectLikeRepetition(purchase.getTitle(),purchase.getPisbn(),purchase.getPissn(),purchase.getEisbn(),purchase.getEissn());
            if(likeRepetition.size()>0){
                String id = likeRepetition.get(0).getId();
                return new Result(true,"疑似重复，确定荐购吗？",id);
            }else{
                return new Result(true,"完全不重复");
            }
        }
    }

    @Override
    public List<Purchase> queryPuchaseByTitle(String title) {
        return purchaseMapper.queryPuchaseByTitle('%'+title+'%');
    }

    @Override
    public Result shenhe(String id) {
        purchaseMapper.shenhe(id);
        return new Result(true,"提交审核成功！");
    }


    @Override
    public Result selectType() {

        String pid="8b808bfb64dsa28a0164dsae0bb10111";
        List<Param> tlist=paramMapper.selectType(pid);
        return new Result(true,"查到类型了",tlist);
    }

    @Override//载体类型
    public Result selectCarrier() {
        String pid="8b808afb64dsa28a0164dsae0bb10003";
        List<Param> clist=paramMapper.selectCarrier(pid);
        return new Result(true,"载体类型",clist);
    }

    @Override//学科
    public Result selectSubject() {
        String pid="8a808bf264a102ba0164a10ac1fc0007";
        List<Param> slist=paramMapper.selectCarrier(pid);
        return new Result(true,"学科",slist);
    }

    @Override//国家
    public Result selectCountry() {
        String pid="8a808bfb64dsa28a0164dsae0bb10003";
        List<Param> clist=paramMapper.selectCarrier(pid);
        return new Result(true,"国家",clist);
    }

    @Override//语种
    public Result selectLanguage() {
        String pid="8b808bfb64bsa28a0164dsae0bb10003";
        List<Param> alist=paramMapper.selectCarrier(pid);
        return new Result(true,"语种",alist);
    }

    @Override//货币
    public Result selectCurrency() {
        String pid="8a808bf264a102ba0164a108eb0e0003";
        List<Param> clist=paramMapper.selectCarrier(pid);
        return new Result(true,"货币",clist);
    }

    @Override
    public Result selectPubStatus() {
        String pid="8a808bfb64daa28a0164dsae0bb10003";
        List<Param> clist=paramMapper.selectCarrier(pid);
        return new Result(true,"出版状态",clist);
    }

    @Override
    public Result queryMyPurchase() {
        Integer reviewStatus=0;
        Integer reviewStatus1=3;

        List<Purchase> plist=purchaseMapper.selectByreviewStatus(reviewStatus,reviewStatus1);
        return new Result(true,"我的荐购列表",plist);
    }

    @Override
    public Result querysPurchase() {
        Integer sid=1;
        List slist=purchaseMapper.selectByReviewstatus(sid);
        return new Result(true,"荐购审核列表",slist);
    }

    @Override
    public Result queryConfirmPurchase() {
        //审核状态
        Integer sid=1;//确认审核  审核的是审核完成的数据
        List slist=purchaseMapper.selectByReviewstatus(sid);
        return new Result(true,"荐购确认列表",slist);
    }

    @Override
    public Result purchaseAgree(String rid) {
        Integer reviewStatus=0;
        HashMap<String, Integer> map = new HashMap<>();
        map.put("rid",Integer.parseInt(rid));
        map.put("reviewStatus",reviewStatus);
        purchaseMapper.updateReviewStatus(map);
        return new Result(true,"同意");
    }

    @Override
    public List<Purchase> queryPuchase() {
        return purchaseMapper.queryPuchase();
    }

    @Override
    public List<PurchaseWithBLOBs> selectPuchase() {
        return purchaseMapper.selectPuchase();
    }

    /**
     * 批量荐购
     * @param list
     * @return
     */
    @Override
    public List<Purchase> queryRepetition(List<Purchase> list) {

        ArrayList<Purchase> arrayList = new ArrayList<>();
        //循环集合里面的每一个对象，进行查重
        for (Purchase purchase:list) {

            List<Purchase> purchaseList=purchaseMapper.selectRepetition(purchase.getTitle(),purchase.getPisbn(),purchase.getPissn(),purchase.getEisbn(),purchase.getEissn());
            if(purchaseList.size()>0){//查到完全重复的值，就不往集合里面追加
                System.out.println("该条数据与荐购数据完全重复！！！");
            }else{//没有完全重复的，也就是疑似重复和不重复两种情况，都可以进行荐购
                List<Purchase> likeRepetition=purchaseMapper.selectLikeRepetition(purchase.getTitle(),purchase.getPisbn(),purchase.getPissn(),purchase.getEisbn(),purchase.getEissn());
                arrayList.add(purchase);
            }
        }
        return arrayList;
    }

    //审核确认  荐购完成，修改数据的状态
    @Override
    public void purchaseComplete(String id) {

        purchaseMapper.purchaseComplete(id);
    }

    @Override
    public void purchaseReject(String id) {
        purchaseMapper.purchaseReject(id);
    }

    @Override
    public Result selectPurchaseById(String id) {

        Purchase purchase=purchaseMapper.selectPurchaseById(id);
        return new Result(true,"查询成功",purchase);
    }


}
